package com.emellend.api.grid;

import java.util.List;
import com.emellend.api.R;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class Grid1 extends Activity {
	
	private List<ResolveInfo> mApps;
	private GridView mGridView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loadApps();
		setContentView(R.layout.grid_1);
		mGridView = (GridView) this.findViewById(R.id.myGrid);
		mGridView.setAdapter(new AppsAdapter());
	}

	/**
	 * 找到所有在系统的launch里有图标的应用
	 */
	private void loadApps() {
		Intent mainIntent=new Intent(Intent.ACTION_MAIN);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		mApps=getPackageManager().queryIntentActivities(mainIntent, 0);
	}
	
	public class AppsAdapter extends BaseAdapter{
		public AppsAdapter() {}

		public int getCount() {
			return mApps.size();
		}

		public Object getItem(int position) {
			return mApps.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i;
			if (convertView==null) {
				convertView=new ImageView(Grid1.this);
				i=(ImageView) convertView;
				i.setScaleType(ScaleType.FIT_CENTER);
				i.setLayoutParams(new GridView.LayoutParams(50, 50));
			}else {
				i=(ImageView) convertView;
			}
			ResolveInfo info=mApps.get(position);
			i.setImageDrawable(info.loadIcon(getPackageManager()));
			return i;
		}
		
	}
}

